package com.esd.csair;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.TopLevelWindow;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class test {
	public static void main(String[] args) {

		long l = System.currentTimeMillis();
		// TODO Auto-generated method stub
		// http://b2c.csair.com/B2C40/checkLogin/jaxb/checkLogin.ao
		WebClient webClient = new WebClient(BrowserVersion.CHROME);// 设置浏览器的User-Agent
		webClient.setJavaScriptTimeout(10000);// 设置JS执行的超时时间
		webClient.getOptions().setThrowExceptionOnScriptError(false);// 当JS执行出错的时候是否抛出异常
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);// 当HTTP的状态非200时是否抛出异常
		webClient.getOptions().setTimeout(30000);// 设置“浏览器”的请求超时时间
		webClient.getOptions().setCssEnabled(false);// 是否启用CSS
		webClient.getOptions().setJavaScriptEnabled(true); // 很重要，启用JS
		webClient.waitForBackgroundJavaScript(30000);// 设置JS后台等待执行时间
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 很重要，设置支持AJAX
		System.out.println(System.currentTimeMillis() - l);

		try {
			WebWindow aa=webClient.openWindow(new URL("http://www.baidu.com"), "百度");
			WebWindow aa1=webClient.openWindow(new URL("http://www.baidu.com"), "百度");
			WebWindow cc=webClient.openWindow(new URL("http://www.csair.com/cn/index.shtml"), "南航首页");
			
			WebWindow bb=webClient.openWindow(new URL("http://b2c.csair.com/B2C40/modules/bookingnew/manage/login.html"), "南航");
			System.out.println("==================================================");
			List<WebWindow> ws = webClient.getWebWindows();
			List<TopLevelWindow> list = webClient.getTopLevelWindows();
			for (TopLevelWindow w : list) {
				System.out.println(w.getName());
			}
			
			HtmlPage page= (HtmlPage) webClient.getWebWindowByName("百度").getEnclosedPage();
			
			System.out.println(page.asXml());
			
		} catch (FailingHttpStatusCodeException | IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
}
